<?php
/**
 * Project: Recipe
 * File: SauceArticleAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-5-5
 * Time: 上午10:59
 */


class SauceArticleAction extends CommonAction{

	function index(){

		import("ORG.Util.Page");

		$saucecategory = M('SauceCategory');
		$saucecategoryinfo = $saucecategory->field('id,name')->select();
		$this->assign('saucecategoryinfo', $saucecategoryinfo);

		$saucearticle = D('SauceArticle');

		if(isset($_POST['search'])){
			if(!empty($_POST['key'])){
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
					case 3 :
						$datas['alias'] = array('like',"%{$_POST['key']}%");
						break;
				}
				if(isset($_GET['cid'])){
					$datas['recipecategoryid'] = $_GET['cid'];
				}
				$total = $saucearticle->where($datas)->count();
				$Page = new Page($total, C('PAGENUM'));
				$show = $Page->show();
				$list = $saucearticle->relation(true)->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			}else{
				$this->error('搜索内容不能为空', __APP__.'/SpecialArticle/index');
			}
		}else{

			if(!isset($_GET['cid'])){
				$total = $saucearticle->count();
				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $saucearticle->relation(true)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			}else{
				$total = $saucearticle->where('recipecategoryid='.$_GET['cid'])->count();
				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $saucearticle->relation(true)->where('recipecategoryid='.$_GET['cid'])->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			}

		}

		$this->assign('list',$list);// 赋值数据集

		$this->assign('show',$show);// 赋值分页输出

		$this->display();
	}

	function addrecommend(){
		if(isset($_GET['cid']) && isset($_GET['id'])){
			if(!empty($_GET['cid']) && !empty($_GET['id'])){
				$sauce = M('SauceArticle');
				$map['recipecategoryid'] = $_GET['cid'];
				$map['id'] = $_GET['id'];
				if($sauce->where($map)->setField('recommend',1)){
					$this->success('推荐成功');
				}else{
					$this->error('推荐失败');
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function delrecommend(){
		if(isset($_GET['cid']) && isset($_GET['id'])){
			if(!empty($_GET['cid']) && !empty($_GET['id'])){
				$sauce = M('SauceArticle');
				$map['recipecategoryid'] = $_GET['cid'];
				$map['id'] = $_GET['id'];
				if($sauce->where($map)->setField('recommend',0)){
					$this->success('取消推荐成功');
				}else{
					$this->error('取消推荐失败');
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	/**
	 * 删除菜谱
	 */
	function del(){
		if(!empty($_POST['actions'])){
			$srcdir = './Public/Content/SauceArticle/';
			$saucearticle= D('SauceArticle');
			$recipenutritions = M('RecipeNutritions');
			$recipemain = M('RecipeMain');
			$recipeaccessory = M('RecipeAccessory');
			$recipecondiment = M('RecipeCondiment');
			$saucecomments = M('SauceComments');
			foreach($_POST['actions'] as $key=>$value){
				$saucearticleinfo = $saucearticle->relation(true)->find($value);
				if(!empty($saucearticleinfo['image'])){
					unlink($srcdir.'Big_'.$saucearticleinfo['image']);
					unlink($srcdir.'Small_'.$saucearticleinfo['image']);
				}
					$delrecipe['recipetypeid'] = $saucearticleinfo['recipetypeid'];
					$delrecipe['recipecategoryid'] = $saucearticleinfo['recipecategoryid'];
					$delrecipe['id'] = $value;
					$num = $saucearticle->where($delrecipe)->delete($value);

					$deletes['recipetypeid'] = $saucearticleinfo['recipetypeid'];
					$deletes['recipecategoryid'] = $saucearticleinfo['recipecategoryid'];
					$deletes['recipearticleid'] = $value;
					$recipemain->where($deletes)->delete();
					$recipeaccessory->where($deletes)->delete();
					$recipecondiment->where($deletes)->delete();

					$del['sectionid'] = $saucearticleinfo['recipetypeid'];
					$del['categoryid'] = $saucearticleinfo['recipecategoryid'];
					$del['articleid'] = $value;
					$recipenutritions->where($del)->delete();

					$saucecomments->where('categoryid='.$saucearticleinfo['recipecategoryid'].' AND articleid='.$value)->delete();
			}
			if($num>0){
				$this->success('删除菜谱成功', __APP__.'/SauceArticle/index/cid/'.$saucearticleinfo['saucecategoryid']);
			}else{
				$this->error('删除菜谱失败');
			}
		}else{
			$this->error('请先选择菜谱');
		}
	}

}